package com.bw.paper.mvp.view.Activity;

import android.graphics.Color;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.bw.paper.mvp.view.Fragment.LiveFragment;
import com.bw.paper.mvp.view.Fragment.MineFragment;
import com.bw.paper.mvp.view.Fragment.NewsFragment;
import com.bw.paper.mvp.view.Fragment.PeopleFragment;
import com.bw.paper.mvp.view.Fragment.VideoFragment;
import com.bw.paper.R;
import com.bw.paper.mvp.base.view.BaseActivity;
import com.bw.paper.mvp.contact.FrameContact;
import com.bw.paper.mvp.di.DaggerFrameComponent;
import com.bw.paper.mvp.di.FrameModules;
import com.bw.paper.mvp.presenter.FramePresenter;
import com.bw.paper.widget.BottomButton;

import java.util.Map;

import constant.DownLoadBy;
import model.UpdateConfig;
import update.UpdateAppUtils;

public class FragmActivity extends BaseActivity<FramePresenter>
        implements BottomButton.SelectListener, FrameContact.IFrameView {

    private NewsFragment newsFragment;
    private PeopleFragment peopleFragment;
    private VideoFragment videoFragment;
    private LiveFragment liveFragment;
    private MineFragment mineFragment;


    private BottomButton news,people,video,live,mine;
    @Override
    public View addStatusView() {
        return f(R.id.fragme_linl);
    }


    @Override
    public void onSelect(int id) {
        if (id == R.id.bottom1){
            people.clearSelect();
            video.clearSelect();
            live.clearSelect();
            mine.clearSelect();
            Colee(Color.WHITE);
        }
        if (id == R.id.bottom2){
            news.clearSelect();
            video.clearSelect();
            live.clearSelect();
            mine.clearSelect();
            Colee(Color.WHITE);
        }
        if (id == R.id.bottom3){
            people.clearSelect();
            news.clearSelect();
            live.clearSelect();
            mine.clearSelect();
            Colee(Color.WHITE);
        }
        if (id == R.id.bottom4){
            people.clearSelect();
            video.clearSelect();
            news.clearSelect();
            mine.clearSelect();
            Colee(Color.WHITE);
        }
        if (id == R.id.bottom5){
            people.clearSelect();
            video.clearSelect();
            live.clearSelect();
            news.clearSelect();
            Colee(Color.RED);
        }

                FragmentManager supportFragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
                if (newsFragment != null) {
                    fragmentTransaction.hide(newsFragment);
                }
                if (peopleFragment != null) {
                    fragmentTransaction.hide(peopleFragment);
                }
                if (videoFragment != null) {
                    fragmentTransaction.hide(videoFragment);
                }
                if (liveFragment != null) {
                    fragmentTransaction.hide(liveFragment);
                }
                if (mineFragment != null) {
                    fragmentTransaction.hide(mineFragment);
                }
                switch (id) {
                    case R.id.bottom1:
                        fragmentTransaction.show(newsFragment);
                        break;
                    case R.id.bottom2:
                        if (peopleFragment == null) {
                            peopleFragment = new PeopleFragment();
                            fragmentTransaction.add(R.id.fragm_linl, peopleFragment);
                        } else {
                            fragmentTransaction.show(peopleFragment);
                        }
                        break;
                    case R.id.bottom3:
                        if (videoFragment == null) {
                            videoFragment = new VideoFragment();
                            fragmentTransaction.add(R.id.fragm_linl, videoFragment);
                        } else {
                            fragmentTransaction.show(videoFragment);
                        }

                        break;
                    case R.id.bottom4:
                        if (liveFragment == null) {
                            liveFragment = new LiveFragment();
                            fragmentTransaction.add(R.id.fragm_linl, liveFragment);
                        } else {
                            fragmentTransaction.show(liveFragment);
                        }

                        break;
                    case R.id.bottom5:
                        if (mineFragment == null) {
                            mineFragment = new MineFragment();
                            fragmentTransaction.add(R.id.fragm_linl, mineFragment);
                        } else {

                            fragmentTransaction.show(mineFragment);
                        }

                        break;
                    default: {
                    }
                }
                fragmentTransaction.commit();



    }


    @Override
    public int bindlayout() {
        return R.layout.activity_fragm;
    }

    @Override
    public void initView() {
        news = findViewById(R.id.bottom1);
        people = findViewById(R.id.bottom2);
        video = findViewById(R.id.bottom3);
        live = findViewById(R.id.bottom4);
        mine = findViewById(R.id.bottom5);


    }

    @Override
    public void inject() {
        DaggerFrameComponent.builder().frameModules(new FrameModules(this)).build()
                .injectFrame(this);

    }

    @Override
    public void initData() {

        news.setContent("新闻").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.xwh)
                .setSelImg(R.drawable.xwhong)
                .setListener(this::onSelect)
                .setShowPoint(false);

        people.setContent("人名号").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.rmhh)
                .setSelImg(R.drawable.rmhhong)
                .setListener(this::onSelect)
                .setShowPoint(false);

        video.setContent("视频").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.sph)
                .setSelImg(R.drawable.sphong)
                .setListener(this::onSelect)
                .setShowPoint(false);

        live.setContent("直播").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.zbh)
                .setSelImg(R.drawable.zbhong)
                .setListener(this::onSelect)
                .setShowPoint(false);
        mine.setContent("我的").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.wdh)
                .setSelImg(R.drawable.wdhong)
                .setListener(this::onSelect)
                .setShowPoint(false);

        newsFragment = new NewsFragment();
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragm_linl, newsFragment);
        fragmentTransaction.commit();

    }
private long exitTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK ){
            if(System.currentTimeMillis() - exitTime > 2000){
                Toast.makeText(this,"在点就退出", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
                return true;
            }else{
                finish(); //结束当前activity
            }
        }


        return super.onKeyDown(keyCode, event);

    }

    @Override
    public Void refresh(Map<String, Object> resultmap) {
        return null;
    }
}